from PyQt5.Qt import *
import datetime

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("上课倒计时")
        self.resize(325, 120)
        self.setup_ui()

    def setup_ui(self):
        # date1 = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        # print("当前时间:", date1,type(date1))
        # date2 = r'2021-11-08 20:00:00'
        # print("上课时间:", date2,type(date2))

        self.label = QLabel(self)
        self.label.move(50, 10)
        self.label.resize(300,15)
        self.label.setStyleSheet("font-size:12px;color:black")

        self.label2 = QLabel(self)
        self.label2.setText('输入上课时间:')
        self.label2.move(50, 37)
        self.label2.setStyleSheet("font-size:12px;color:black")

        self.label3 = QLabel(self)
        self.label3.move(145, 65)
        self.label3.resize(340,15)
        self.label3.setStyleSheet("font-size:12px;color:black")

        self.textEdit=QTextEdit(self)
        self.textEdit.setText(QDateTime.currentDateTime().toString("yyyy-MM-dd")+' 20:00:00')
        self.textEdit.move(140,33)
        self.textEdit.resize(140,25)

        self.btn = QPushButton(self)
        self.btn.setText("显示倒计时")
        self.btn.move(48, 60)

        self.date1=''
        self.date2=''

        self.btn.clicked.connect(self.hd)

        timer = QTimer(self)
        timer.timeout.connect(self.showtime)
        timer.start(1000)

    def showtime(self):
        datetime = QDateTime.currentDateTime()
        self.text = datetime.toString("yyyy-MM-dd hh:mm:ss")
        self.label.setText('当前时间:\t'+self.text)

    def hd(self):
        timer2 = QTimer(self)
        timer2.timeout.connect(self.showtime2)
        timer2.start(1000)

    def showtime2(self):
        print(123)
        info1 = self.text
        print("info1:\t",info1,type(info1))
        info2 = self.textEdit.toPlainText()
        print("info2:\t",info2,type(info2))

        self.date1 = datetime.datetime.strptime(info1, "%Y-%m-%d %H:%M:%S")
        self.date2 = datetime.datetime.strptime(info2, "%Y-%m-%d %H:%M:%S")

        print("倒计时:\t",self.date2 - self.date1,type(str(self.date2-self.date1)))
        self.label3.setText(str(self.date2-self.date1))


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())